Attribute VB_Name = "Logger"
Public frmForm     As Form1
Public tags() As TagObject
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub WriteLog(ByVal data As String)
    ' Todo Add time
    frmForm.Logger.AddItem Time & ":" & data, 0
    Dim out As Boolean
    out = WriteToFile(Now & ":" & data, App.Path & "/Log", "log.txt")
    
End Sub

Sub WriteDebug(ByVal debugStr As String)


End Sub
Public Sub SetTag(ByVal tagName As String, ByVal TagValue As String)
    ' Replace tag
    Dim lblName As String
    Dim ctlLabel As Control
    lblName = Right(tagName, Len(tagName) - InStr(1, tagName, "\"))
    
    For Each ctlLabel In Form1.Controls
        If ctlLabel.Name = lblName Then
            Exit For
        End If
    Next ctlLabel
            
    If Not ctlLabel Is Nothing Then
        ctlLabel.Text = TagValue
    End If
    
End Sub

Public Function gTagDb(ByVal tagName As String) As TagObject
   Dim i As Integer
   Dim found As Boolean
   Dim idx As Integer
   If IsEmpty(tags) Then
        idx = 0
    Else
        idx = UBound(tags)
    End If
   
   If idx = -1 Then idx = 0
   found = False
   
   For i = 1 To UBound(tags)
    If tags(i).tagName = tagName Then
        found = True
        Set gTagDb = tags(i)
    End If
        
   Next
   
   If Not found Then
      ReDim Preserve tags(idx + 1)
      Dim obj As New TagObject
      obj.tagName = tagName
      Set tags(idx + 1) = obj
      Set gTagDb = obj
   End If
End Function

Public Function GetTag(ByVal tagName As String) As String

    ' Replace tag
    Dim lblName As String
    Dim ctlLabel As Control
    lblName = Right(tagName, Len(tagName) - InStr(1, tagName, "/"))
    
    For Each ctlLabel In Form1.Controls
        If ctlLabel.Name = lblName Then
            Exit For
        End If
    Next ctlLabel
            
    If Not ctlLabel Is Nothing Then
        GetTag = ctlLabel.Text
    End If
    
End Function



Public Function WriteToFile(strLineToWrite As String, _
                            strPathName As String, _
                            strFileName As String) As Boolean
   
    Dim FileN As Integer
    Dim FileExists As String
    Dim DirExists As String
      
    FileN = FreeFile
   
'--Check for the directory existance
    DirExists = Dir(strPathName, vbDirectory)
   
    If DirExists = "" Then
        MkDir (strPathName)
    End If
   
    FileExists = Dir(strPathName & "\" & strFileName, vbNormal)
 
'--Check for the file existance
'--If it's not there create it and write the line of text into it
    If FileExists = "" Then
         Open strPathName & "\" & strFileName For Output As FileN
            Print #FileN, strLineToWrite
        Close FileN
    Else
'--If it is there then append data to end of file
        Open strPathName & "\" & strFileName For Append As FileN
        
            Print #FileN, strLineToWrite
        Close FileN
    End If
   
    WriteToFile = True
 
End Function


